capture log close success
log using abs-rel-q2-success.txt, name(success) replace text

//  program:    abs-rel-q2-success.do
//  task:		analysis of absolute and relative inequality judgments
// 	input:		exp1.dta						
//	output:		none
//  project:    MEDEC experiment 1
//  author:     sam harper \ 25mar2013

//  #0
//  program setup

version 12
set linesize 80
clear all
macro drop _all


// #1
// bring in experiment 1 data

use exp1.dta, clear
datasignature confirm

// #3
// Scenario: decreasing abs, increasing relative

* first test for EMM by scenario status
regress q2success i.time##i.treatment##i.mag if scennum==2 | scennum==6, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.195, so drop product term 
* and adjust for scenario
regress q2success i.time##i.treatment i.mag if scennum==2 | scennum==6, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q2s26

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("How successful in reducing inequality?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q2s26, replace) ///
	ytitle("1 (Not at all) ... 7 (Very successful)") ///
	note("Scenarios 2 and 6: Decreasing absolute, increasing relative inequality", size(small))

* marginal effects
margins, over(time) dydx(treatment mag) cformat(%4.3f)


// #3
// Scenario: constant absolute, increasing relative

* first test for EMM by scenario status
regress q2success i.time##i.treatment##i.mag if scennum==7 | scennum==8, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.768, so drop product term 
* and adjust for scenario
regress q2success i.time##i.treatment i.mag if scennum==7 | scennum==8, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q2s78

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("How successful in reducing inequality?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q2s78, replace) ///
	ytitle("1 (Not at all) ... 7 (Very successful)") ///
	note("Scenarios 7 and 8: Constant absolute, increasing relative inequality", size(small))

* marginal effects
margins, over(time) dydx(treatment mag) cformat(%4.3f)


// #3
// Scenario: decreasing abs, constant relative

* first test for EMM by scenario status
regress q2success i.time##i.treatment##i.mag if scennum==3 | scennum==4, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.427, so drop product term 
* and adjust for scenario	
regress q2success i.time##i.treatment i.mag if scennum==3 | scennum==4, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q2s34

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("How successful in reducing inequality?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q2s34, replace) ///
	ytitle("1 (Not at all) ... 7 (Very successful)") ///
	note("Scenarios 3 and 4: Decreasing absolute, constant relative inequality", size(small))

* marginal effects
margins, over(time) dydx(treatment mag) cformat(%4.3f)


// #4
// Scenario: decreasing abs, decreasing relative

* first test for EMM by scenario status
regress q2success i.time##i.treatment##i.mag if scennum==1 | scennum==5, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.373, so drop product term 
* and adjust for scenario
regress q2success i.time##i.treatment i.mag if scennum==1 | scennum==5, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q2s15

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("How successful in reducing inequality?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q2s15, replace) ///
	note("Scenarios 1 and 5: Decreasing absolute, decreasing relative inequality", size(small))
	
* marginal effects
margins, over(time) dydx(treatment mag) cformat(%4.3f)


// #5
// Scenario: increasing abs, increasing relative

* first test for EMM by scenario status
regress q2success i.time##i.treatment##i.mag if scennum==9 | scennum==10, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.379, so drop product term 
* and adjust for scenario
regress q2success i.time##i.treatment i.mag if scennum==9 | scennum==10, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q2s910

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("How successful in reducing inequality?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q2s910, replace) ///
	note("Scenarios 9 and 10: Increasing absolute, increasing relative inequality", size(small))
margins, over(time) dydx(treatment mag) cformat(%4.3f)

//#6
// table of all effect estimates
esttab q2s26 q2s78 q2s34 q2s15 q2s910 using q2est.rtf, ///
	b(%5.3f) ci(%4.2f) ///
	keep(1.time 1.treatment 1.time#1.treatment 1.mag _cons) ///
	collabels(Coef. 95\%CI) nostar not replace compress ///
	title("Program success")
	
	
// #7
// put all results in a single graph, collapsing common scenarios
// and adjusting for scenario

* collapse scenarios
recode scennum (2 6 = 1 "Difference decreases, Ratio increases") ///
	(7 8 =2 "Difference constant, Ratio increases") ///
	(3 4 = 3 "Difference decreases, Ratio constant") ///
	(1 5 = 4 "Difference decreases, Ratio decreases") ///
	(9 10 = 5 "Difference increases, Ratio increases"), gen(grscen)
	
* predicted margins for inconsistent scenarios
qui regress q2success i.grscen##(i.time##i.treatment) i.mag if grscen<4, ///
	cluster(subject) cformat(%4.3f) vsquish
qui margins time#treatment, over(grscen)
marginsplot, xsc(r(-.2 1.2)) xtitle("") by(grscen) ///
	byopts(rows(1) title("Scenarios with inconsistent absolute and relative inequality trends", size(medsmall)) ///
	legend(off) graphregion(lcolor(white) fcolor(white))) ///
	ylab(1(1)7) subtitle(, size(vsmall) fcolor(white) lcolor(white)) ///
	ytitle("How successful in reducing inequality?" "1 (Not at all) ... 7 (Very successful)", ///
	size(vsmall)) name(con, replace) fysize(42)

* predicted margins for consistent scenarios	
qui regress q2success i.grscen##(i.time##i.treatment) i.mag if grscen>3 ///
	& grscen<11, cluster(subject) cformat(%4.3f) vsquish
qui margins time#treatment, over(grscen)
marginsplot, xsc(r(-.2 1.2)) xtitle(Time) by(grscen) ///
	byopts(rows(1) title("Scenarios with consistent absolute and relative inequality trends", size(medsmall)) ///
	graphregion(lcolor(white) fcolor(white) margin(l=20 r=20))) ///
	legend(order(3 "shown raw data and difference measure of inequality" 4 "shown raw data and ratio measure of inequality") ///
	cols(1) rowgap(0.5) size(small) symxsize(8) ///
	title("Experimental condition:", size(small)) width(95)) ///
	subtitle(, size(vsmall) fcolor(white) lcolor(white)) ///
	ytitle("How successful in reducing inequality?" "1 (Not at all) ... 7 (Very successful)", ///
	size(vsmall)) name(incon, replace) ylab(1(1)7)
	
graph combine con incon, rows(2) xsize(3.5) graphregion(lcolor(white) fcolor(white))

* predicted margins for exporting to R graphs	
qui regress q2success i.grscen##(i.time##i.treatment) i.mag if grscen<11, ///
	cluster(subject)
margins time#treatment, over(grscen)

log close success
exit

